#include "seat.h"
#include "util.h"
#include <stdlib.h>

static void setcursor(struct wl_listener *listener, void *data)
{
#if 1
    Seat *_ = wl_container_of(listener, _, request_cursor);
    /* This event is raised by the seat when a client provides a cursor image */
    struct wlr_seat_pointer_request_set_cursor_event *event = data;
    /* If we're "grabbing" the cursor, don't use the client's image */
    /* TODO still need to save the provided surface to restore later */
    if (_->server->cursor_mode != CurNormal)
        return;
    /* This can be sent by any client, so we check to make sure this one is
     * actually has pointer focus first. If so, we can tell the cursor to
     * use the provided surface as the cursor image. It will set the
     * hardware cursor on the output that it's currently on and continue to
     * do so as the cursor moves between outputs. */
    if (event->seat_client == _->seat->pointer_state.focused_client)
        wlr_cursor_set_surface(_->server->cursor->cursor, event->surface,
                event->hotspot_x, event->hotspot_y);
#endif
}

static void setsel(struct wl_listener *listener, void *data)
{
#if 0
    /* This event is raised by the seat when a client wants to set the selection,
     * usually when the user copies something. wlroots allows compositors to
     * ignore such requests if they so choose, but in dwl we always honor
     */
    struct wlr_seat_request_set_selection_event *event = data;
    wlr_seat_set_selection(seat, event->source, event->serial);
#endif
}

static void setpsel(struct wl_listener *listener, void *data)
{
#if 0
    /* This event is raised by the seat when a client wants to set the selection,
     * usually when the user copies something. wlroots allows compositors to
     * ignore such requests if they so choose, but in dwl we always honor
     */
    struct wlr_seat_request_set_primary_selection_event *event = data;
    wlr_seat_set_primary_selection(seat, event->source, event->serial);
#endif
}

static void requeststartdrag(struct wl_listener *listener, void *data)
{
#if 0
    struct wlr_seat_request_start_drag_event *event = data;

    if (wlr_seat_validate_pointer_grab_serial(seat, event->origin,
            event->serial))
        wlr_seat_start_pointer_drag(seat, event->drag, event->serial);
    else
        wlr_data_source_destroy(event->drag->source);
#endif
}

static void startdrag(struct wl_listener *listener, void *data)
{
#if 0
    struct wlr_drag *drag = data;

    if (!drag->icon)
        return;

    drag->icon->data = wlr_scene_subsurface_tree_create(layers[LyrNoFocus], drag->icon->surface);
    motionnotify(0);
    wl_signal_add(&drag->icon->events.destroy, &drag_icon_destroy);
#endif
}

Seat *seat_create(Server* server)
{
    Seat *_ = ecalloc(1, sizeof(*_));
    _->server = server;

    _->seat = wlr_seat_create(server->dpy, "seat0");
    wl_signal_add(&_->seat->events.request_set_cursor, &_->request_cursor);
    _->request_cursor.notify = setcursor;
    wl_signal_add(&_->seat->events.request_set_selection, &_->request_set_sel);
    _->request_set_sel.notify = setsel;
    wl_signal_add(&_->seat->events.request_set_primary_selection, &_->request_set_psel);
    _->request_set_psel.notify = setpsel;
    wl_signal_add(&_->seat->events.request_start_drag, &_->request_start_drag);
    _->request_start_drag.notify = requeststartdrag;
    wl_signal_add(&_->seat->events.start_drag, &_->start_drag);
    _->start_drag.notify = startdrag;

    return _;
}

void seat_destroy(Seat *seat)
{
    free(seat);
}
